package com.ww.web;

import com.alibaba.fastjson.JSON;
import com.ww.dao.DeliveryDao;
import com.ww.dao.impl.DeliveryDaoImpl;
import com.ww.entity.Delivery;
import com.ww.entity.User;
import com.ww.util.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Pattern;

@WebServlet("/delivery/*")
public class DeliveryServlet extends HttpServlet {
    private DeliveryDao deliveryDao=new DeliveryDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String baseURL = req.getContextPath();//获取项目的基本路径
        String uri = req.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        if ("add.do".equals(path)) {
            add(req, resp);
            return;
        }else if ("gladd.do".equals(path)) {
            List<Delivery> deliveryList = deliveryDao.findAll();
            req.setAttribute("deliveryList", deliveryList);
            req.getRequestDispatcher(baseURL + "/gladdress.jsp").forward(req, resp);
            return;
        }else if ("delete.do".equals(path)) {
            String id = req.getParameter("id");
            int delete = deliveryDao.delete(id);
            System.out.println("delete = " + delete);
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.write("");
            out.close();
            return;
        }else if ("updateaddress.do".equals(path)) {
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String phone = req.getParameter("phone");
            String address = req.getParameter("address");
            HttpSession session = req.getSession();
            String user_id = null;
            if (session.getAttribute("user_id")!=null) {
                user_id = session.getAttribute("user_id").toString();
            }
            Delivery delivery=new Delivery();
            delivery.setDelivery_id(id);
            delivery.setDelivery_address(address);
            delivery.setDelivery_name(name);
            delivery.setDelivery_phone(phone);
            delivery.setUser_id(user_id);
            int update = deliveryDao.update(delivery);
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.write("");
            out.close();
            return;
        }else if ("update.do".equals(path)) {
            String id = req.getParameter("id");
            Delivery delivery = deliveryDao.findById(id);
            req.setAttribute("delivery",delivery);
            req.getRequestDispatcher(baseURL+"/addressupdate.jsp").forward(req,resp);
            return;
        }else if ("pay.do".equals(path)) {
            String uuid = UUIDUtils.getUUID();
            String salary = req.getParameter("salary");
            req.setAttribute("salary",salary);
            req.setAttribute("uuid",uuid);
            req.getRequestDispatcher(baseURL+"/pay.jsp").forward(req,resp);
            return;
        } else {
            throw new RuntimeException("请求错误");
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String uuid = UUIDUtils.getUUID();
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        HttpSession session = req.getSession();
        String user_id = null;
        if (session.getAttribute("user_id")!=null) {
            user_id = session.getAttribute("user_id").toString();
        }
        Delivery delivery=new Delivery();
        delivery.setDelivery_id(uuid);
        delivery.setDelivery_address(address);
        delivery.setDelivery_name(name);
        delivery.setDelivery_phone(phone);
        delivery.setUser_id(user_id);
        int save = deliveryDao.save(delivery);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.write("");
        out.close();
        return;
    }
}
